package com.biboheart.adapter.adapter.impl;

import java.util.*;

final class ListIndex {
    private final Map<String, Integer> sourceListSizeHash;
    private final Map<String, String> listPathContrast = new HashMap<>();
    private final Map<String, Integer> targetPathHash = new HashMap<>();
    private final List<Integer> targetIndexList = new ArrayList<>();

    public ListIndex(Map<String, Integer> sourceListSizeHash) {
        this.sourceListSizeHash = sourceListSizeHash;
    }

    public void putContrast(String targetPath, String sourcePath) {
        listPathContrast.put(targetPath, sourcePath);
    }

    public Integer sourceListSize(String targetPath) {
        if (!listPathContrast.containsKey(targetPath)) {
            return 0;
        }
        String sourcePath = listPathContrast.get(targetPath);
        if (!sourceListSizeHash.containsKey(sourcePath)) {
            return 0;
        }
        return sourceListSizeHash.get(sourcePath);
    }

    public Map<String, Integer> getSourceListIndex(String targetContainsIndexPath) {
        return null;
    }


    public List<String> containsIndexPath(String sourcePath, boolean containSelf) {
        if (null == sourcePath || targetIndexList.isEmpty()) {
            return null;
        }
        if (!containSelf && !sourcePath.contains(".")) {
            return Collections.singletonList(sourcePath);
        }
        String[] split = sourcePath.split("\\.");
        List<String> pathList = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        List<List<String>> listList = new ArrayList<>();
        for (String str : split) {
            listList.add(new ArrayList<>());
            keyList.add(str);
            String key = String.join(".", keyList);
            if (key.equals(sourcePath) && !containSelf) {
                for (List<String> list : listList) {
                    list.add(str);
                }
                break;
            }
            int len = targetIndexList.get(targetPathHash.get(key)) + 1;
            int size = listList.size();
            for (int i = 0; i < size; i++) {
                List<String> list = listList.get(i * len);
                for(int j = 0; j < len; j ++) {
                    if (j == 0) {
                        list.add(String.valueOf(j));
                    } else {
                        List<String> newList = new ArrayList<>(list);
                        newList.set(newList.size() - 1, String.valueOf(j));
                        listList.add((i * len) + j, newList);
                    }
                }
            }
        }
        return pathList;
    }

    public void updateTargetPathIndex(String path, Integer index) {
        if (!targetPathHash.containsKey(path)) {
            targetIndexList.add(index);
            targetPathHash.put(path, targetIndexList.size() - 1);
        } else {
            targetIndexList.set(targetPathHash.get(path), index);
        }
    }

    public Map<String, Integer> getTargetIndexMap() {
        Map<String, Integer> map = new HashMap<>();
        if (targetPathHash.isEmpty()) {
            return map;
        }
        for (Map.Entry<String, Integer> entry : targetPathHash.entrySet()) {
            map.put(entry.getKey(), targetIndexList.get(entry.getValue()));
        }
        return map;
    }
}
